package cn.wuyu.baselibrary.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

import androidx.annotation.ArrayRes;
import androidx.annotation.AttrRes;
import androidx.annotation.BoolRes;
import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DimenRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.IntegerRes;
import androidx.annotation.NonNull;
import androidx.annotation.RawRes;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;



/**
 * AndroidResource
 * Created by xzm on 2016/2/24.
 */
public class ResUtils {
//    final Resource self = this;


    /* Constructors */


    /* Public Methods */
    public static Resources getResources() {
        return ContextHolder.getContext().getResources();
    }

    public static int getColor(@ColorRes int resID) {
        return ContextCompat.getColor(ContextHolder.getContext(), resID);
    }

    public static ColorStateList getColorStateList(@ColorRes int resID) {
        return ContextCompat.getColorStateList(ContextHolder.getContext(), resID);
    }

    public static Drawable getDrawable(@DrawableRes int resID) {
        return ContextCompat.getDrawable(ContextHolder.getContext(), resID);
    }

    public static File[] getExternalCacheDirs() {
        return ContextCompat.getExternalCacheDirs(ContextHolder.getContext());
    }

    public static File[] getExternalFilesDirs(String type) {
        return ContextCompat.getExternalFilesDirs(ContextHolder.getContext(), type);
    }

    public static File[] getObbDirs() {
        return ContextCompat.getObbDirs(ContextHolder.getContext());
    }

    public static String getString(@StringRes int resID) {
        return ContextHolder.getContext().getResources().getString(resID);
    }

    public static String getString(@StringRes int resID, Object... formatArgs) {
        return ContextHolder.getContext().getResources().getString(resID, formatArgs);
    }

    public static String[] getStringArray(@ArrayRes int resID) {
        return ContextHolder.getContext().getResources().getStringArray(resID);
    }

    public static int getInteger(@IntegerRes int resID) {
        return ContextHolder.getContext().getResources().getInteger(resID);
    }

    public static int[] getIntArray(@ArrayRes int resID) {
        return ContextHolder.getContext().getResources().getIntArray(resID);
    }

    public static boolean getBoolean(@BoolRes int resID) {
        return ContextHolder.getContext().getResources().getBoolean(resID);
    }

    public static float getDimension(@DimenRes int resID) {
        return ContextHolder.getContext().getResources().getDimension(resID);
    }

    public static int getDimensionPixelSize(@DimenRes int resID) {
        return ContextHolder.getContext().getResources().getDimensionPixelSize(resID);
    }

    public static TypedArray obtainTypedArray(@ArrayRes int resID) {
        return ContextHolder.getContext().getResources().obtainTypedArray(resID);
    }

    public static TypedArray obtainAttributes(AttributeSet set, int[] attrs) {
        return ContextHolder.getContext().getResources().obtainAttributes(set, attrs);
    }

    public static DisplayMetrics getDisplayMetrics() {
        return ContextHolder.getContext().getResources().getDisplayMetrics();
    }

    /* Properties */


    /* Overrides */


    /* Delegates */


    /* Private Methods */
    @ColorInt
    public static int getAttrColor(@AttrRes int attr) {
//        @NonNull Context context,
        TypedArray a = AppUtil.getAppContext().obtainStyledAttributes(null, new int[]{attr});
        try {
            return a.getColor(0, 0);
        } finally {
            a.recycle();
        }
    }
    public static Drawable getAttrDrawable( @AttrRes int attr) {
        TypedArray a = AppUtil.getAppContext().obtainStyledAttributes(null, new int[]{attr});
        try {
            return a.getDrawable(0);
        } finally {
            a.recycle();
        }
    }
    public static String getRawString(@NonNull Context context, @RawRes int rawId) throws IOException {
        InputStream is = context.getResources().openRawResource(rawId);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        reader.close();
        return sb.toString();
    }
}